package hsb.ruoyi.common.config.security;

import com.usthe.sureness.matcher.DefaultPathRoleMatcher;
import com.usthe.sureness.matcher.PathTreeProvider;
import com.usthe.sureness.mgt.SurenessSecurityManager;
import com.usthe.sureness.processor.DefaultProcessorManager;
import com.usthe.sureness.processor.Processor;
import com.usthe.sureness.processor.support.NoneProcessor;
import com.usthe.sureness.provider.SurenessAccountProvider;
import com.usthe.sureness.provider.ducument.DocumentAccountProvider;
import com.usthe.sureness.subject.SubjectCreate;
import com.usthe.sureness.subject.SubjectFactory;
import com.usthe.sureness.subject.SurenessSubjectFactory;
import io.quarkus.runtime.Startup;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.enterprise.context.ApplicationScoped;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

@ApplicationScoped
@Startup
public class JwtSurenessConfig {

    private static final Logger logger = LoggerFactory.getLogger(JwtSurenessConfig.class);

    //public static final String SUPPORT_JAX_RS = "jax-rs";


    JwtSubjectJaxRsCreator jwtSubjectJaxRsCreator;

    public JwtSurenessConfig(JwtSubjectJaxRsCreator jwtSubjectJaxRsCreator) {
        this.jwtSubjectJaxRsCreator=jwtSubjectJaxRsCreator;
        this.init();
    }


    /**
     * 这个是只支持 jwt方式，相当于只用这个框架做了，路径和觉得的匹配，jwt的创建，维护还用原来的方式
     *
     * */
    private void init() {

        // process init
        // account provider
        SurenessAccountProvider accountProvider = new DocumentAccountProvider();

        List<Processor> processorList = new LinkedList<>();
        NoneProcessor noneProcessor = new NoneProcessor();
        processorList.add(noneProcessor);
        JwtProcessor jwtProcessor = new JwtProcessor();
        processorList.add(jwtProcessor);
      /*  PasswordProcessor passwordProcessor = new PasswordProcessor();
        passwordProcessor.setAccountProvider(accountProvider);
        processorList.add(passwordProcessor);
        DigestProcessor digestProcessor = new DigestProcessor();
        digestProcessor.setAccountProvider(accountProvider);
        processorList.add(digestProcessor);*/
        DefaultProcessorManager processorManager = new DefaultProcessorManager(processorList);
        if (logger.isDebugEnabled()) {
            logger.debug("DefaultProcessorManager init");
        }

        // pathRoleMatcher init
        // pathTree resource provider

        //构造路径和角色的对应关系
        PathTreeProvider pathTreeProvider = new MyPathTreeProvider();

        DefaultPathRoleMatcher pathRoleMatcher = new DefaultPathRoleMatcher();

        pathRoleMatcher.setPathTreeProvider(pathTreeProvider);

        pathRoleMatcher.buildTree();
        if (logger.isDebugEnabled()) {
            logger.debug("DefaultPathRoleMatcher init");
        }

        // SubjectFactory init
        SubjectFactory subjectFactory = new SurenessSubjectFactory();
        List<SubjectCreate> subjectCreates;

        subjectCreates = Arrays.asList(
                new NoneSubjectJaxRsCreator(),
                jwtSubjectJaxRsCreator
        );

        subjectFactory.registerSubjectCreator(subjectCreates);
        if (logger.isDebugEnabled()) {
            logger.debug("SurenessSubjectFactory init");
        }

        // surenessSecurityManager init
        SurenessSecurityManager securityManager = SurenessSecurityManager.getInstance();
        securityManager.setPathRoleMatcher(pathRoleMatcher);
        securityManager.setSubjectFactory(subjectFactory);
        securityManager.setProcessorManager(processorManager);
        if (logger.isDebugEnabled()) {
            logger.debug("SurenessSecurityManager init");
        }
    }
}